#==========================================================================
# KCalDAV
#--------------------------------------------------------------------------
# (c) 2009  Kumaran Santhanam
#
# This project is released under the GNU General Public License.
# Please see the file COPYING for more details.
#--------------------------------------------------------------------------
# Project makefile
#==========================================================================

VERSION = 1.2.1

#==========================================================================
# DEFINITIONS
#==========================================================================
OUTDIR    = _build
SOURCE	  = src
LIBCALDAV = libcaldav-0.6.2.tar.gz

ECHO    = echo
CP      = cp -f
MKDIR   = mkdir -p
TAR     = tar
PATCH   = patch
PACKAGE = kcaldav-$(VERSION)


#==========================================================================
# RULES
#==========================================================================
all: build

build: $(OUTDIR)/libcaldav.a
	cd $(OUTDIR) ; cmake -D KCALDAV_VERSION=$(VERSION) -D CMAKE_INSTALL_PREFIX=/usr ../$(SOURCE) ; make

buildforrpm: $(OUTDIR)/libcaldav.a
	cd $(OUTDIR) ; cmake -D KCALDAV_VERSION=$(VERSION) -D CMAKE_INSTALL_PREFIX=$(CALDAV_RPM_BUILDROOT)/usr ../$(SOURCE) ; make

debug: $(OUTDIR)/libcaldav.a
	cd $(OUTDIR) ; cmake -D KCALDAV_DEBUG=1 -D KCALDAV_VERSION=$(VERSION) -D CMAKE_INSTALL_PREFIX=/usr ../$(SOURCE) ; make

install:
	cd $(OUTDIR) ; make install

install/strip:
	cd $(OUTDIR) ; make install/strip

uninstall:
	cd $(OUTDIR) ; make uninstall

clean:
	rm -rf $(OUTDIR)

tar:
	@$(ECHO) TAR: $(OUTDIR)/$(PACKAGE).tar.gz
	@$(MKDIR) $(OUTDIR)
	@rm -f $(OUTDIR)/$(PACKAGE); ln -s .. $(OUTDIR)/$(PACKAGE)
	@cd $(OUTDIR); tar --exclude='*/_*' --owner=root --group=root --mtime="`date '+%F %T'`" -czf $(PACKAGE).tar.gz $(PACKAGE)/*

rpm: tar
	@$(ECHO) RPM: $(PACKAGE)
	@cd $(OUTDIR); rpmbuild -tb $(PACKAGE).tar.gz

deb: tar
	@$(ECHO) DEB: $(PACKAGE)
	@rm -fr $(OUTDIR)/dpkg
	@$(MKDIR) $(OUTDIR)/dpkg
	@tar -C $(OUTDIR)/dpkg -xzf $(OUTDIR)/$(PACKAGE).tar.gz
	@$(MKDIR) $(OUTDIR)/dpkg/$(PACKAGE)/debian
	@for i in pkg/*.dpkg; do \
            cp $$i $(OUTDIR)/dpkg/$(PACKAGE)/debian/$$(basename $$i .dpkg); \
        done
	@cp COPYING $(OUTDIR)/dpkg/$(PACKAGE)/debian/copyright
	@cd $(OUTDIR)/dpkg/$(PACKAGE); dpkg-buildpackage -b -uc
	@cp $(OUTDIR)/dpkg/*.deb $(OUTDIR)


.PHONY: all build debug install uninstall clean tar rpm

$(OUTDIR)/libcaldav.a:
	@$(ECHO) LIB: $@
	@$(MKDIR) $(OUTDIR)
	@$(TAR) -C $(OUTDIR) -xzf lib/$(LIBCALDAV)
	@$(PATCH) -d $(OUTDIR) -p0 -i ../lib/$(LIBCALDAV:%.tar.gz=%.patch)
	@(cd $(OUTDIR)/libcaldav* && \
		./configure --with-pic && \
		$(MAKE) $(MAKECMDFLAGS) -C src)
	@$(CP) $(OUTDIR)/libcaldav*/src/.libs/libcaldav.a $(OUTDIR)
	@$(CP) $(OUTDIR)/libcaldav*/src/caldav.h $(OUTDIR)
